﻿
/// <reference path="../../Plugin/datepicker/js/jquery.ui.datepicker.js" />
/// <reference path="../../Plugin/datepicker/css/jquery.ui.core.css" />
/// <reference path="../../Plugin/datepicker/css/jquery.ui.datepicker.css" />
/// <reference path="../../Plugin/datepicker/css/jquery.ui.theme.css" />
/// <reference path="../Pages/Product/BuyingGroup.js" />



//-----------------------------------------------------------
// Author:      thuphuong.tran
// Create Date: 28/10/2010
// Description: Xử lý show popup danh sach Admin
//-----------------------------------------------------------
ShowListItemAuction = new function () {
    this.productBuyNow = "";
    this.itemID = 0;
    this.realItemID = -1;
    this.fromDate = "";
    this.toDate = "";
    this.name = "";
    this.categoryID = -1;
    this.itemTypeID = -1;
    this.categoryID = -1;
    this.status = -1;
    this.m_isPostBackListItem = true;
    this.m_objPageListItem = new VtcPaging("divPagingListProductBuyNowPopup", "ShowListItemAuction.bindListItem", "pagingCss", 5, CONSTANT.PAGE_DISPLAY);
    this.m_idShowListItemAuction = "divShowListItemAuction";
    this.showPopup = function (_productBuyNow) {
        ShowListItemAuction.productBuyNow = _productBuyNow;
        if ($("#" + this.m_idShowListItemAuction).length == 0) {
            $("body").append("<div id='" + this.m_idShowListItemAuction + "'></div>");
            $("#" + ShowListItemAuction.m_idShowListItemAuction).setTemplateURL("/Template/Popup/ShowListItemAuction.htm");
            $("#" + ShowListItemAuction.m_idShowListItemAuction).processTemplate(null);
            ShowListItemAuction.setRowNumber("#" + ShowListItemAuction.m_idShowListItemAuction);
            var top = (($(window).height() / 2) - ($("#divPopup_ShowListItemAuction").outerHeight() / 2)) - 80;
            var left = (($(window).width() / 2) - ($("#divPopup_ShowListItemAuction").outerWidth() / 2));
            if (top < 0) top = 0;
            if (left < 0) left = 0;
            // IE6 fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) top = top + $(window).scrollTop();

            $("#divPopup_ShowListItemAuction").css({
                top: top + 'px',
                left: left + 'px'
            });

            $(".content_info_left", "#divPopup_ShowListItemAuction").css({
                'text-align': 'left',
                'width': 80 + 'px'
            });

            $(".content_info_right", "#divPopup_ShowListItemAuction").css({
                'text-align': 'left',
                'width': 120 + 'px'
            });

            $("#txtFromDateListItem").datepicker({
                changeMonth: true,
                changeYear: true
            });
            $("#txtToDateListItem").datepicker({
                changeMonth: true,
                changeYear: true
            });

            var _date = new Date();
            //bind dữ liệu cho ToDate
            $("#txtToDateListItem").val(ShowListItemAuction.convertDate(_date));

            //bind dữ liệu cho FromDate
            _date.setDate(_date.getDate() - 365);
            $("#txtFromDateListItem").val(ShowListItemAuction.convertDate(_date));

            $("#txtRealItemIDListItem").val("");
            $("#txtRealItemIDListItem").focus();
            $("#txtItemNameListItem").val('');
            this.bindListItem(1, ShowListItemAuction.m_objPageListItem.pageSize);
        }

        try {
            $("#divPopup_ShowListItemAuction").draggable({ handle: $("#divPopup_ShowListItemAuction .barpopup") });
            $("#divPopup_ShowListItemAuction .barpopup").css({ cursor: 'move' });
        } catch (e) { /* requires jQuery UI draggables */ }


    };

    this.bindListItem = function (_cur, _ps) {
        Loading.showProcess();
        ShowListItemAuction.toDate = $("#txtToDateListItem").val();
        ShowListItemAuction.fromDate = $("#txtFromDateListItem").val();

        if (this.m_isPostBackListItem) {
            $("#divListProductBuyNowPopup").html(CONSTANT.TAG_IMAGE_LOADING);
        }
        else {
            Loading.show("#divListProductBuyNowPopup");
        }

        $.ajax({
            type: "GET",
            url: "/handler/HandlerProduct.ashx",
            data: { productBuyNow: ShowListItemAuction.productBuyNow, realItemID: ShowListItemAuction.realItemID, name: ShowListItemAuction.name, categoryID: ShowListItemAuction.categoryID, itemTypeID: ShowListItemAuction.itemTypeID, fromDate: ShowListItemAuction.fromDate, toDate: ShowListItemAuction.toDate, status: ShowListItemAuction.status, currentPage: _cur, pageSize: _ps, t: 'GetListItemForProductBuyNow' },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                ShowListItemAuction.processData(data, _cur);
                Loading.closeProcess();
            }
        });

    };

    this.processData = function (data, _cur) {
        $("#spTotalItem").html('(' + data.TotalRecord.toString() + ')');
        $("#divListProductBuyNowPopup").setTemplateURL("/Template/Popup/ShowListItemAuctionSub.htm");
        $("#divListProductBuyNowPopup").processTemplate(data);
        ShowListItemAuction.setStyleRow("#divListProductBuyNowPopup");
        ShowListItemAuction.m_objPageListItem.bindPaging(_cur, data.TotalRecord);
        if (data.TotalRecord <= ShowListItemAuction.m_objPageListItem.pageSize) {
            $("#divPagingListProductBuyNowPopup").attr('style', 'display:none');
        }
        else {
            $("#divPagingListProductBuyNowPopup").attr('style', 'display:block');
        }
    };

    this.searchListItem = function () {
        if (!this.validateSearch()) return;
        ShowListItemAuction.realItemID = $("#txtRealItemIDListItem").val();
        ShowListItemAuction.fromDate = $("#txtFromDateListItem").val();
        ShowListItemAuction.toDate = $("#txtToDateListItem").val();
        ShowListItemAuction.status = $("#ddlStatusListItem").val();
        ShowListItemAuction.name = $("#txtItemNameListItem").val();
        ShowListItemAuction.categoryID = $("#ddlCategoryListItem").val();

        ShowListItemAuction.itemTypeID = $("#ddlItemTypeIDListItem").val();
        ShowListItemAuction.bindListItem(1, ShowListItemAuction.m_objPageListItem.pageSize);
    };

    $("#btnSearchProductBuyNow").live('click', function () {
        ShowListItemAuction.searchListItem();
    });

    // Kiem tra du lieu truoc khi tim kiem
    this.validateSearch = function () {
        if (!IsNumeric($("#txtRealItemID").val())) {
            jAlert(MESSAGE.errorRealItemIDNotIsNumberic, MESSAGE.title, function () {
                $("#txtRealItemID").focus();
            });
            return false;
        }

        if ($("#txtFromDate").val() == '') {
            jAlert(MESSAGE.datetimenull, MESSAGE.title, function () {
                $("#txtFromDate").focus();
            });
            return false;
        }
        if ($("#txtToDate").val() == '') {
            jAlert(MESSAGE.datetimenull, MESSAGE.title, function () {
                $("#txtToDate").focus();
            });
            return false;
        }

        if (Untils.isDate($("#txtFromDate").val()) < 0) {
            jAlert(MESSAGE.errorFormatDate, MESSAGE.title, function () {
                $("#txtFromDate").focus();
            });
            return false;
        }

        if (Untils.isDate($("#txtToDate").val()) < 0) {
            jAlert(MESSAGE.errorFormatDate, MESSAGE.title, function () {
                $("#txtToDate").focus();
            });
            return false;
        }
        if (!Untils.DateCompare($("#txtFromDate").val(), $("#txtToDate").val())) {
            jAlert(MESSAGE.erroDateTime, MESSAGE.title, function () {
                $("#txtToDate").focus();
            });
            return false;
        }
        return true;
    };

    this.validateDigit = function (evt) {
        try {
            var keyCode = evt.keyCode ? evt.keyCode : evt.which;
            var key = String.fromCharCode(keyCode);
            if (key && (/[a-zA-Z]/.test(key) || /[{}=|_<>,\?~`ơư!@#$%^&*().+-]/.test(key))) {
                return (false);
            }
            return (true);
        } catch (err) {
            return true;
        }
    };

    $("#aSelectProductBuyNow").live('click', function () {
        _itemID = $(this.parentNode).attr("id").substring(6);
        Pages_ListBuyingGroup.productBuyNow = Pages_ListBuyingGroup.productBuyNow + _itemID.toString() + ",";
        ShowListItemAuction.productBuyNow = ShowListItemAuction.productBuyNow + _itemID.toString() + ",";

        Pages_ListBuyingGroup.bindListProductBuyNow(Pages_ListBuyingGroup.productBuyNow);
        ShowListItemAuction.bindListItem(1, ShowListItemAuction.m_objPageListItem.pageSize);
    });

    $("#divAddProductBuyNowPopup").live('click', function () {
        $("#divListProductBuyNowPopup #trItem").find("input[checked='true']").each(function () {
            var m_itemID = $(this).attr("id").substring(3);
            Pages_ListBuyingGroup.productBuyNow = Pages_ListBuyingGroup.productBuyNow + m_itemID + ",";
            ShowListItemAuction.productBuyNow = ShowListItemAuction.productBuyNow + m_itemID + ",";
        });
        Pages_ListBuyingGroup.bindListProductBuyNow(Pages_ListBuyingGroup.productBuyNow);
        ShowListItemAuction.bindListItem(1, ShowListItemAuction.m_objPageListItem.pageSize);
    });

    this.close = function () {
        if ($("#" + this.m_idShowListItemAuction).length != 0) {
            $("#" + this.m_idShowListItemAuction).remove();
        }
    };

    this.setRowNumber = function (m_selParent) {
        var m_stt = 0;
        $('#divSTT', m_selParent).each(function () {
            m_stt++;
            $(this).text(m_stt);
        });
    };

    this.setStyleRow = function (m_selParent) {
        var m_stt = 0;
        $('#divItem', m_selParent).each(function () {
            m_stt++;
            if ((m_stt % 2) == 0) {
                $(this.parentNode).addClass('even');
            }
            else {
                $(this.parentNode).addClass('odd');
            }
        });
    };

    this.convertDate = function (expDate) {
        return expDate.getDate() + '/' + (expDate.getMonth() + 1) + '/' + expDate.getFullYear();
    };

    $("#cbxAllProductBuyNowPopup").live("click", function () {
        if (this.checked) {
            $(this).parent().parent().parent().find("input[type='checkbox']").attr("checked", "" + this.checked);
        }
        else {
            $(this).parent().parent().parent().find("input[type='checkbox']").removeAttr("checked");
        }
    });

}